package com.hospital.controller;

import com.hospital.entity.User;
import com.hospital.entity.UserDepartment;
import com.hospital.service.UserDepartmentService;
import com.hospital.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 科室操作相关controller
 */
@Controller
@CrossOrigin
@RequestMapping("UserDepartment")
public class UserDepartmentController {
    @Autowired
    private JsonUtil jsonUtil;
    @Autowired
    private UserDepartmentService userDepartmentService;

    /**
     * 添加用户科室方法
     *
     * @param userId       用户id
     * @param departmentId 科室id
     * @return 修改状态
     */
    @ResponseBody
    @RequestMapping("InsertDepartment")
    public Boolean InsertDepartment(Integer userId, @RequestParam("departmentId[]") Integer[] departmentId) {
        Integer state = 0;
        for (int i = 0; i < departmentId.length; i++) {
            UserDepartment userDepartment = new UserDepartment(userId, departmentId[i], "1", null);
            state = userDepartmentService.InsertDepartment(userDepartment);
        }
        jsonUtil.setRestate(state == 1);
        return jsonUtil.getRestate();
    }

    /**
     * 修改用户科室方法
     *
     * @param userId       用户id
     * @param departmentId 科室id
     * @return 修改状态
     */
    @ResponseBody
    @RequestMapping("UpdateDepartment")
    public Boolean UpdateDepartment(Integer userId, @RequestParam("departmentId[]") Integer[] departmentId) {
        //操作状态
        Integer state = 0;
        for (int i = 0; i < departmentId.length; i++) {
            userDepartmentService.DeleteDepartment(userId);
        }
        for (int i = 0; i < departmentId.length; i++) {
            UserDepartment userDepartment = new UserDepartment(userId, departmentId[i], "1", null);
            state = userDepartmentService.InsertDepartment(userDepartment);
        }
        jsonUtil.setRestate(state == 1);
        return jsonUtil.getRestate();
    }

    /**
     * 用户姓名与科室查询
     *
     * @param name       用户姓名
     * @param department 科室
     * @return 用户信息
     */
    @ResponseBody
    @RequestMapping("SelectByNameDepartment")
    public List<User> SelectByNameDepartment(String name, String department) {
        List<User> users = userDepartmentService.SelectByNameDepartment("%" + name + "%", department);
        return users;
    }
}
